//
//  HYFNetworkTools.swift
//  MyWeibo
//
//  Created by 黄勇锋 on 16/7/22.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD

enum HYFNetworkToolsType:String{
    
    case GET = "GET"
    case POST = "POST"
}


class HYFNetworkTools: AFHTTPSessionManager {
    
    static let shardTools:HYFNetworkTools = {
        
       let tools = HYFNetworkTools()
        
        // 设置反序列化 格式
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    func request(type:HYFNetworkToolsType,urlString:String,parameters:AnyObject?,success:(response:AnyObject?)->(),failure:(error:NSError)->()){
        
        //get 请求
        if type == .GET {
            
            GET(urlString, parameters: parameters, progress: nil, success: { (_, res) in
                
                success(response: res)
                
                }, failure: { (_, err) in
                    
                failure(error: err)
                    
            })
  
        }else{
            
            //post 请求
            POST(urlString, parameters: parameters, progress: nil, success: { (_, res) in
                
                success(response: res)
                
                }, failure: { (_, err) in
                    
                failure(error: err)
            }) 
        }
    }
}


extension HYFNetworkTools {
    
    
    //请求 用户账号
    func oauthLoadUserAccount(code:String,success:(response:AnyObject?)->(),failure:(error:NSError)->()){
        
        //请求 urlString
        let urlString = "https://api.weibo.com/oauth2/access_token"
        //请求参数
        let pramas = [
            
            "client_id": APPKEY,
            "client_secret": APPSECRET,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": APPREDIRECT_URI
        ]

        request(HYFNetworkToolsType.POST, urlString: urlString, parameters: pramas, success: success, failure: failure)
        
    }
    
    //请求用户信息
    func oauthLoadUserInfo(userAccount:HYFUserAccount,success:(response:AnyObject?)->(),failure:(error:NSError)->()){
        
        //请求 url
        let urlString = "https://api.weibo.com/2/users/show.json"
        //请求 参数
        let pramas = [
            
            "access_token":userAccount.access_token!,
            "uid":userAccount.uid!
        ]
        
        request(HYFNetworkToolsType.GET, urlString: urlString, parameters: pramas, success: success, failure: failure)
        
    }
}

//请求微博首页数据
extension HYFNetworkTools {
    
    func homeLoadData(sinceId:Int64,maxId:Int64,success:(response:AnyObject?)->(),failure:(error:NSError)->()){
        
        //请求 urlString
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let params = ["access_token":HYFUserAccountViewModel.sharedTools.accessToken!,
        
                       "since_id":"\(sinceId)",
                       "max_id":"\(maxId)"
        
                     ]
        
        request(HYFNetworkToolsType.GET, urlString: urlString, parameters: params, success: success, failure: failure)
        
    }
}


extension HYFNetworkTools {
    
    //发送文字微博
    func composeUpdate(status:String,success:(response:AnyObject?)->(),failure:(error:NSError)->()){
        
        //请求的 urlString
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        //请求参数
        let params = [
                      "access_token":HYFUserAccountViewModel.sharedTools.accessToken!,
                      "status":status
                    ]
     
        request(.POST, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    
    //发送图片微博
    func composeUpload(status:String,imageList:[UIImage],success:(response:AnyObject?)->(),failure:(error:NSError)->()){
        
        //请求 urlString
        let urlString = "https://upload.api.weibo.com/2/statuses/upload.json"
        //请求参数
        let params = [
            
            "access_token":HYFUserAccountViewModel.sharedTools.accessToken!,
            "status":status
            
        ]
        
        //发送请求
        POST(urlString, parameters: params, constructingBodyWithBlock: { (formdata) in
            
            for i in 0..<imageList.count{
                
                let data = UIImagePNGRepresentation(imageList[i])!
                formdata.appendPartWithFileData(data, name: "pic", fileName: "oooo", mimeType: "application/octet-stream")
                
            }

            
            }, progress: nil, success: { (_, response) in
                
                  success(response: response)
                
            }) { (_, error) in
                
                failure(error: error)

        }
    }
}

